package jsu.yym.tiktok.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jsu.yym.tiktok.pojo.Video;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.repository.query.Param;

import java.util.List;

@Mapper
public interface VideoMapper extends BaseMapper<Video> {
    //根据username查找用户上传的视频
    @Select("select * from video where username = #{username}")
    List<Video> getVideoByUserName(String username);
    //根据username和state获取video
    @Select("SELECT * FROM video WHERE username = #{username} AND state = #{state}")
    List<Video> getVideoByUserNameAndState(@Param("username") String username, @Param("state") int state);
    //根据地址获取视频
//    @Select("select * from video where address = #{address} and state =#(state}")
//    List<Video> getVideoByAddress(String address,int state);
    @Select("select * from video where address = #{address} and state = #{state}")
    List<Video> getVideoByAddress(String address, int state);


    @Select("select * from video where state = #{state}")
    List<Video> getVideoByState(int state);

    @Select({
            "<script>",
            "SELECT * FROM video",
            "WHERE pk_id IN",
            "<foreach item='id' collection='pkIdList' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<Video> getVideosByPkIds(@Param("pkIdList") List<Integer> pkIdList);

    @Select({
            "<script>",
            "SELECT * FROM video",
            "WHERE username IN",
            "<foreach item='username' collection='usernames' open='(' separator=',' close=')'>",
            "#{username}",
            "</foreach>",
            "<if test='state != null'>",
            "AND state = #{state}",
            "</if>",
            "</script>"
    })
    List<Video> getVideosByUsernamesAndState(@Param("usernames") List<String> usernames, @Param("state") Integer state);


    @Select({
            "<script>",
            "SELECT label FROM video",
            "WHERE pk_id IN",
            "<foreach item='id' collection='pkIdList' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<String> getLabelsByPkIds(@Param("pkIdList") List<Integer> pkIdList);


    @Select("select label from video where pk_id = #{pk_id}")
    String getLabelByPkId(int pk_id);

    @Select("select * from video where pk_id = #{pk_id}")
    Video getVideoByPkId(int pk_id);

    @Select("select url from video where pk_id = #{pk_id}")
    String getUrlByPkId(int pk_id);

    @Update("UPDATE video SET likes = #{likes} WHERE pk_id = #{pk_id}")
    int updateLikesByPkId(@org.apache.ibatis.annotations.Param("likes")int likes, @org.apache.ibatis.annotations.Param("pk_id") int pk_id);

    @Update("UPDATE video SET collections = #{collections} WHERE pk_id = #{pk_id}")
    int updateCollectionsByPkId(@org.apache.ibatis.annotations.Param("collections")int collections, @org.apache.ibatis.annotations.Param("pk_id") int pk_id);


    @Update("UPDATE video SET shares = #{shares} WHERE pk_id = #{pk_id}")
    int updateSharesByPkId(@org.apache.ibatis.annotations.Param("shares")int shares, @org.apache.ibatis.annotations.Param("pk_id") int pk_id);

    @Update("UPDATE video SET state = #{state} WHERE pk_id = #{pk_id}")
    int updateStateByPkId(@org.apache.ibatis.annotations.Param("state") int state, @org.apache.ibatis.annotations.Param("pk_id") int pk_id);



}
